package cz.muni.fi.pa165.ddtroops.web.exceptionhandlers;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.exception.DefaultExceptionHandler;
import org.apache.tiles.impl.CannotRenderException;
import org.springframework.orm.jpa.JpaSystemException;

public class DDTroopsExceptionHandler extends DefaultExceptionHandler {
    public Resolution handleConstrainViolationException(JpaSystemException exc, HttpServletRequest request, HttpServletResponse response) {

        Pattern p = Pattern.compile("/(?<name>\\w+)/.*");
        Matcher matcher = p.matcher(request.getServletPath());
        return new RedirectResolution(matcher.replaceAll("/${name}/all?error=constraint"));
    }
//    public Resolution handleConstrainViolationException(CannotRenderException exc, HttpServletRequest request, HttpServletResponse response) {
//        return new ForwardResolution("/error.jsp");
//    }
}